package com.yzf.wisecoder.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.ViewDbReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.ViewDbResp;
import com.yzf.wisecoder.wisecoderservice.service.ViewDatasourceService;
import com.yzf.wisecoder.wisecoderservice.service.ViewDbService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewDatasourceEntity;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewDbEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-12 10:20:48
 */
@RestController
@RequestMapping("sys/viewdb")
@AllArgsConstructor
@Api(value = "模板数据源控制", tags = "模板数据源控制")
public class ViewDbController {

    private final ViewDbService viewDbService;
    private final ViewDatasourceService viewDatasourceService;

    /**
     * 获取项目的数据源列表
     *
     * @param projectId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取项目的数据源列表")
    public Response<List<ViewDbResp>> list(@RequestParam Long projectId) {
        List<ViewDbEntity> list = viewDbService.queryList(projectId);
        List<ViewDbResp> viewDbResps = BeanCopyUtils.copyList(list, ViewDbResp.class);
        return Response.buildSuccessResponseWithData(viewDbResps);
    }


    /**
     * 获取项目数据源信息
     *
     * @param dbId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取项目数据源信息")
    public Response<ViewDbResp> info(@RequestParam Long dbId) {
        ViewDbEntity viewDb = viewDbService.getById(dbId);
        ViewDbResp viewDbResp = new ViewDbResp();
        BeanUtils.copyProperties(viewDb, viewDbResp);
        return Response.buildSuccessResponse();
    }

    /**
     * 保存或修改模板数据源
     *
     * @param viewDbReq
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存或修改模板数据源")
    public Response save(@RequestBody ViewDbReq viewDbReq) {
        ValidatorUtils.validateEntity(viewDbReq);
        ViewDbEntity viewDbEntity = new ViewDbEntity();
        BeanUtils.copyProperties(viewDbReq, viewDbEntity);
        viewDbService.saveOrUpdateViewDb(viewDbEntity);

        return Response.buildSuccessResponse();
    }

    /**
     * 删除数据源信息
     *
     * @param dbId
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除数据源信息")
    public Response delete(@RequestBody Long dbId) {
        List<ViewDatasourceEntity> viewDatasourceEntityList = viewDatasourceService.queryByDbId(dbId);
        if (CollectionUtil.isNotEmpty(viewDatasourceEntityList)) {
            return Response.buildErrorResponse("该数据源已使用,不能删除");
        }
        viewDbService.removeByIds(Lists.newArrayList(dbId));
        return Response.buildSuccessResponse();
    }

}
